package com.cj.library.widget.dialog;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

import com.cj.library.widget.recyclerview.adapter.BaseViewHolder;
import com.cj.library.widget.utiles.DensityUtils;

/**
 * Created by Administrator on 2016-06-24.
 */
public class PopuWindowUtile {
    private PopupWindow popup;
    private View contentView;
    private Activity mAty;
    private BaseViewHolder viewHolder;

    public PopuWindowUtile(Activity aty, @LayoutRes int layoutId) {
        contentView = LayoutInflater.from(aty).inflate(layoutId, null);
        viewHolder = new BaseViewHolder(contentView);

        popup = new PopupWindow(contentView, (int) (DensityUtils.getScreenW(aty) * 0.8), (int) (DensityUtils.getScreenH(aty) * 0.8));
        popup.setOutsideTouchable(true);
        popup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mAty = aty;
    }

    /**
     * 设置框高
     *
     * @param w
     * @param h
     * @return
     */
    public PopuWindowUtile setWH(int w, int h) {
        popup.setWidth(w);
        popup.setHeight(h);
        return this;
    }

    public PopuWindowUtile setOutsideTouchable(boolean able) {
        popup.setOutsideTouchable(able);
        return this;
    }

    public PopuWindowUtile setAnimationStyle(@StyleRes int styleId) {
        popup.setAnimationStyle(styleId);
        return this;
    }


    public PopuWindowUtile setOnDismissListener(PopupWindow.OnDismissListener listener) {
        popup.setOnDismissListener(listener);
        return this;
    }

    /**
     * 侠士Popu窗口
     */
    public void show() {
        show(Gravity.CENTER);
    }

    /**
     * @param gravity 显示位置
     */
    public void show(int gravity) {
        if (popup.isShowing()) {
            popup.dismiss();
        }
        popup.showAtLocation(mAty.getWindow().getDecorView(), gravity, 0, 0);
    }

    public void close() {
        if (popup.isShowing()) {
            popup.dismiss();
        }
    }

    public BaseViewHolder getViewHolder() {
        return viewHolder;
    }
}
